<?php

namespace App\Model;


/**
 * Class Commodity_size
 * @package App\Model
 * @User wuqin
 * @Date 2018/3/29 0029
 * @Time 20:38
 */
class Commodity_size extends Common
{
    /**
     * @var bool
     */
    public $timestamps = false;
    /**
     * @var string
     */
    protected $primaryKey = 'commodity_size_id';
    /**
     * @var string
     */
    protected $table = 'commodity_sizes';

    /**
     * 方法 : add_size
     * @User wuqin
     * @DateTime 2018-03-29 20:38
     * @param $var
     * @return bool
     */
    public function add_size($var)
    {
        $this->commodity_id = $var['commodity_id'];
        $this->commodity_size_name = $var['commodity_size_name'];
        if (!$this->save())
            return false;
        return true;

        $where = array(
            array(
                'where', 'commodity_size_id', '=', $this->commodity_size_id
            )
        );
        $data = $this->get($where, '*', 0, 0);
        if (empty($data)) {
//            $this->set_empty_array_redis('commodity_size:' . $this->commodity_size_id . ':');
            return false;
        }
        $this->set_array($data);
        if (!empty($keys = $this->keys_redis('commodity_size:' . $this->commodity_size_id . ':*'))) {
            return true;
        }
        return false;
    }

    /**
     * 方法 : set_array
     * @User wuqin
     * @DateTime 2018-03-29 20:38
     * @param $data
     * @return bool
     */
    public function set_array($data)
    {
        if (empty($data))
            return false;
        foreach ($data as $key => $value) {
            $this->set_array_redis('commodity_size:' . $value['commodity_size_id'] . ':commodity:' . $value['commodity_id'], $value);
        }
    }

    /**
     * 方法 : update_size
     * @User wuqin
     * @DateTime 2018-03-29 20:38
     * @param $var
     * @return bool
     */
    public function update_size($var)
    {
        $this->commodity_size_name = $var['commodity_size_name'];
        if (!$this->save())
            return false;
        $where = array(
            array(
                'where', 'commodity_size_id', '=', $this->commodity_size_id
            )
        );
        $data = $this->get($where, '*', 0, 0);
        if (empty($data)) {
            $this->set_empty_array_redis('commodity_size:' . $this->commodity_size_id . ':');
            return false;
        }
        $this->set_array($data);
        if (!empty($keys = $this->keys_redis('commodity_size:' . $this->commodity_size_id . ':*'))) {
            return true;
        }
        return false;
    }

    /**
     * 方法 : get_all_size
     * @User wuqin
     * @DateTime 2018-03-29 20:38
     * @return array|bool|mixed
     */
    public function get_all_size()
    {
        $key_index = 'commodity_size:*';
        if (empty($keys = $this->keys_redis($key_index))) {
            $data = $this->get(array(), '*', 0, 0, ['commodity_id', 'asc']);
            if (empty($data))
                $this->set_empty_array_redis('commodity_size:0');
            $this->set_array($data);
        }
        $keys = $this->keys_redis($key_index);
        $data = $this->mget_array_redis($keys);
        return $data;
    }

    /**
     * 方法 : get_size_by_commodity_id
     * @User wuqin
     * @DateTime 2018-03-29 20:38
     * @param $commodity_id
     * @return array|bool|mixed
     */
    public function get_size_by_commodity_id($commodity_id)
    {
        $key_index = 'commodity_size:*:commodity:' . $commodity_id;
        if (empty($keys = $this->keys_redis($key_index))) {
            $where = array(
                array(
                    'where', 'commodity_id', '=', $commodity_id
                )
            );
            $data = $this->get($where, '*', 0, 0);
            if (empty($data))
                $this->set_empty_array_redis('commodity_size:0:commodity:' . $commodity_id);
            $this->set_array($data);
        }
        $keys = $this->keys_redis($key_index);
        $data = $this->mget_array_redis($keys);
        return $data;
    }

    /**
     * 方法 : set_empty
     * @User wuqin
     * @DateTime 2018-03-29 20:38
     * @param string $commodity_size_id
     * @param string $commodity_id
     */
    public function set_empty($commodity_size_id = '0', $commodity_id = '0')
    {
        $this->set_empty_array_redis('commodity_size:' . $commodity_size_id . ':commodity:' . $commodity_id);
    }
}
